;Make all position reference become relative
AutoItSetOption("MouseCoordMode", 0)

WinActivate("[23-st733] eFin Smart Portal ")

Local $file = FileOpen("C:\temp_1\stocklist.txt", 0)
Local $stockSQLFile = FileOpen("C:\temp_1\stock.sql", 1)

If $stockSQLFile = -1 Then
    MsgBox(0, "Error", "Unable to open stock sql file.")
    Exit
EndIf



; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

Local $stockIndex = 1

; Read in lines of text until the EOF is reached
While 1
    Local $stockName = FileReadLine($file)
    If @error = -1 Then ExitLoop
    
	MouseClick("", 62, 122)
	Send($stockName)
	Send("{ENTER}")
	
	Sleep(2000)
	
	MouseClick("", 639, 107)
	Send("01{RIGHT}01{RIGHT}1998")
	MouseClick("", 824, 102)
	
	Sleep(5000)
	
	MouseClick("", 1907, 135)
	
	Sleep(5000)
	
    WinWaitActive("Data View")
	
	MouseClick("", 32, 30)
	Sleep(250)
	MouseClick("", 66, 76)
	Sleep(500)
	
	If WinActivate("Data View") Then
	   MouseClick("", 32, 30)
	   Sleep(250)
	   MouseClick("", 66, 76)
	EndIf
	
	FileDelete("C:\temp_1\price.txt")
	
	WinWaitActive("Export Text File")
	
	Send("!n")
	Send("C:\temp_1\price.txt")
	Send("{ENTER}")
	
	;WinWaitActive("Export Text File")
	;MouseClick("", 111, 92)
	Sleep(1000)
	WinActivate("Export Text File")
	ControlClick("Export Text File", "", "[CLASS:Button; INSTANCE:1]")
    ;Send("!o")
	;Send("{SPACE}")
	
	WinWaitActive("Data View")
	MouseClick("", 487, 10)
	
	WinWaitActive("[23-st733] eFin Smart Portal ")
	
	Local $stockFile = FileOpen("C:\temp_1\price.txt", 0)

	; Check if file opened for reading OK
	If $stockFile = -1 Then
	    MsgBox(0, "Error", "Unable to open stock file.")
	    Exit
	EndIf

    Local $lineIndex = 1
    While 1
	   Local $line = FileReadLine($stockFile)
	   If @error = -1 Then ExitLoop
	   $lineIndex = $lineIndex + 1
	   
	   If $lineIndex > 28 Then
		  Local $lineArr = StringSplit($line, " ")
		  Local $loopCount = Number($lineArr[0])
		  Local $date = ""
		  Local $open = ""
		  Local $high = ""
		  Local $low = ""
		  Local $close = ""
		  Local $smav26 = ""
		  Local $vol = ""
		  For $i = 1 To $loopCount
			 
			 If $lineArr[$i] <> "" Then
				If $date = "" Then
				   $date = $lineArr[$i]
				ElseIf $open = "" Then
				   $open = $lineArr[$i]
				ElseIf $high = "" Then
				   $high = $lineArr[$i]
				ElseIf $low = "" Then
				   $low = $lineArr[$i]
				ElseIf $close = "" Then
				   $close = $lineArr[$i]
				ElseIf $smav26 = "" Then
				   $smav26 = $lineArr[$i]
				ElseIf $vol = "" Then
				   $vol = StringReplace($lineArr[$i], ",", "")
				EndIf
				
			 EndIf
			 
		  Next
		  ConsoleWrite($date & ":" & $open & ":" & $high & ":" & $low & ":" & $close & ":" & $smav26 & ":" & $vol & @CRLF)
		  Local $sql = "INSERT INTO FINANCE.PRICE_HIST ( MARKET_NAME, CURRENCY_PAIR, TRADING_TYPE, TRADING_DATE, OPEN, HIGH, LOW, CLOSE, VOLUME ) VALUES ( "
		  $sql = $sql & "'STOCK', '" & $stockName & "', 'DAILY', to_date('" & $date & "', 'DD/MM/YY')"
		  $sql = $sql & ", " & $open & ", " & $high & ", " & $low & ", " & $close & ", " & $vol & " );"
		  FileWriteLine($stockSQLFile, $sql & @CRLF)

	   EndIf
	   
    WEnd
	   
    FileClose($stockFile)
	
	$stockIndex = $stockIndex + 1
	
	If Mod($stockIndex, 50) = 0 Then
	   
	   MouseClick("", 1909, 9)
	   Sleep( 15000 )
	   
	   ShellExecuteWait("C:\Documents and Settings\Admin\Start Menu\Programs\Online Asset\Smart Portal.appref-ms")
	   
	   WinWaitActive("[23-st733] eFin Smart Portal ")
	   
	   Sleep( 20000 )
	   
	   MouseClick("", 1251, 102, 2)
	   MouseClick("", 375, 31)
	   
	EndIf
	
WEnd

WinActivate("[23-st733] eFin Smart Portal ")
MouseClick("", 1909, 9)

FileClose($file)
FileClose($stockSQLFile)

